#include <cstdio>

const int N = 500005;
int n, g[N], fa[N];
int e_cnt, heads[N];
long long ans, f[N];
char str[N];

struct Edge {
  int v, nxt;
} e[N << 1];

inline void add(int u, int v) { e[++e_cnt].v = v, e[e_cnt].nxt = heads[u], heads[u] = e_cnt; }

void dfs(int x) {
  g[x] = g[fa[x]];
  if (str[x] == '(') g[x] = x;
  if (str[x] == ')') {
    if (g[fa[x]] != 0) {
      f[x] = 1 + f[fa[g[x]]];
      g[x] = g[fa[g[x]]];
    }
  }
  for (int i = heads[x], v; i; i = e[i].nxt) {
    dfs(v = e[i].v);
  }
}

void dfs2(int x) {
  ans ^= x * f[x];
  for (int i = heads[x], v; i; i = e[i].nxt) {
    f[v = e[i].v] += f[x];
    dfs2(v);
  }
}
int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
  freopen("P5658 括号树.in", "r", stdin);
  freopen("P5658 括号树.out", "w", stdout);
#endif
#endif

  scanf("%d", &n);
  scanf("%s", str + 1);
  for (int i = 2, tmp; i <= n; ++i) {
    scanf("%d", &tmp);
    add(tmp, i);
    fa[i] = tmp;
  }
  dfs(1);
  dfs2(1);
  printf("%lld", ans);
  return 0;
}